package com.yunyi.ne.netty;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class WebSocketService {
    private static final Map<String, NettyWebSocketClient> map = new HashMap<>();


    public void sendMessageToServers(String message, String serverUrl) throws Exception {
        NettyWebSocketClient client = null;

        client = NettyWebSocketClientFactory.createClient(serverUrl);
        map.put(serverUrl, client);
        log.info("创建了{}的ws链接", serverUrl);

        if (client != null) {
            client.sendMessage(message);
        }


    }

    public void closeAllWebSocketClients() throws InterruptedException {
        NettyWebSocketClientFactory.closeAllClients();
    }
}